package com.example.animationdrawabledemo;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.drawable.RotateDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    RotateDrawable drawable;
    LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        linearLayout = (LinearLayout) findViewById(R.id.ll);
        LayoutTransition layoutTransition = new LayoutTransition();
        linearLayout.setLayoutTransition(layoutTransition);

        Animator animatorAppear = AnimatorInflater.loadAnimator(this,R.animator.custom_animator_appear);
        layoutTransition.setAnimator(LayoutTransition.APPEARING,animatorAppear);

        Animator animatorDisappear = AnimatorInflater.loadAnimator(this,R.animator.custom_animator_disapper);
        layoutTransition.setAnimator(LayoutTransition.DISAPPEARING,animatorDisappear);


//        ImageView imageView = (ImageView) findViewById(R.id.iv);
//        PropertyValuesHolder rotationHolder = PropertyValuesHolder.ofFloat("rotation",0,360);
//        PropertyValuesHolder scaleXHolder = PropertyValuesHolder.ofFloat("scaleX",1,3);
//        PropertyValuesHolder scaleYHolder = PropertyValuesHolder.ofFloat("scaleY",1,3);
//        PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat("alpha",1,0);
//        ObjectAnimator objectAnimatorSet = ObjectAnimator.ofPropertyValuesHolder(imageView,rotationHolder,scaleXHolder,scaleYHolder,alphaHolder);
//
//        ObjectAnimator objectAnimatorTraslateY = ObjectAnimator.ofFloat(imageView,"translationY",0,600);
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.setDuration(1000);
//        animatorSet.play(objectAnimatorSet).after(objectAnimatorTraslateY);
//        animatorSet.start();

//        TextView textView = (TextView) findViewById(R.id.tv);
//        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.custom_animator_appear);
//        animator.setTarget(textView);
//        animator.start();

//        ObjectAnimator translationAnimator = ObjectAnimator.ofFloat(imageView,"translationY",0,600);
//        ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(imageView,"rotation",0,360);
//        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView,"scaleX",1,3);
//        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView,"scaleY",1,3);
//        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(imageView,"alpha",1,0);
//
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.play(rotateAnimator).after(translationAnimator).with(scaleXAnimator).with(scaleYAnimator).with(alphaAnimator);
//        animatorSet.setDuration(1500);
//        animatorSet.start();

    }

    int level=0;
    Handler handler= new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            level+=100;
            if(level>10000){
                level=0;
            }
            drawable.setLevel(level);
            handler.postDelayed(this,1);
        }
    };

    public static int dip2px(Context context,float dpValue){
        final float scale=context.getResources().getDisplayMetrics().density;
        return (int)(dpValue*scale+0.5f);//加0.5f用于四舍五入
    }

    public static int px2dip(Context context,float pxValue){
        final float scale=context.getResources().getDisplayMetrics().density;
        return (int)(pxValue/scale+0.5f);//加0.5f用于四舍五入
    }

    public static int getScreenWidth(Context context){
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }

    public static int getScreenHeight(Context context){
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels;
    }

    public static float getScreenDensity(Context context){
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.density;
    }

    public void btnOnclick(View view) {
        TextView textView = new TextView(this);
        textView.setOnClickListener(this);
        textView.setText(Math.random()+"");
        linearLayout.addView(textView,0);
    }





    @Override
    public void onClick(View v) {
        linearLayout.removeView(v);
    }
}
